package com.template.config.redis.manager;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.redis.cache.*;

import java.time.Duration;

public class RedisCacheConfigManager extends RedisCacheManager {
    public RedisCacheConfigManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
        super(cacheWriter, defaultCacheConfiguration);
    }

    @NotNull
    @Override
    protected RedisCache createRedisCache(@NotNull String name, RedisCacheConfiguration cacheConfig) {
        int lastIndexOf = StringUtils.lastIndexOf(name, '#');
        if (lastIndexOf > -1) {
            long ttl = Long.parseLong(StringUtils.substring(name, lastIndexOf + 1));
            cacheConfig = cacheConfig.entryTtl(Duration.ofSeconds(ttl));
            name = StringUtils.substring(name, 0, lastIndexOf);
        }
        return super.createRedisCache(name, cacheConfig);
    }
}
